from webob import Response
from webob.exc import HTTPMovedPermanently, HTTPNotFound
from fusion.http.controllers import BaseController
from fusion.db.exceptions import Unauthorized
from fusion.db.profile import Profile

class Index(BaseController):
    def index(self, req):
        db = req.environ['fusion.db']

        # make sure we have a persona
        try:
            my_iid = db.get_my_iid()
            db.fetch(my_iid, '')
        except LookupError: # persona not initialized
            # redirect to admin
            redir_url = 'http://admin.hexlabel.net/setup_persona/'
            return HTTPMovedPermanently(location=redir_url)

        # determine the kind of dataset and redirect
        if req.urlvars.has_key('fusion.hexlabel'):
            type = db.get_type(req.urlvars['fusion.hexlabel'])
            if type:
                if type == 'group':
                    redir_url = 'http://%s.hexlabel.net/group/' % req.urlvars['fusion.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)
                elif type == 'person' or type == 'principle':
                    redir_url = 'http://%s.hexlabel.net/person/' % req.urlvars['fusion.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)
                elif type == 'content':
                    return 'CONTENT[%s]' % req.urlvars['fusion.hexlabel']
                    

        if req.host == 'my.hexlabel.net': 
            redir_url = 'http://%s.hexlabel.net/person/' % my_iid
            return HTTPMovedPermanently(location=redir_url)

        else:
            return HTTPNotFound()


